<template>
  <div>
    <div v-if="shows == 'list'">
      <Header>
        <el-button size="small" type="primary" @click="add">
          新建打印机
        </el-button>
      </Header>
      <Main :data="listData" :footer-lock="totals > 10">
        <el-table-column
          label="打印机名称"
          align="center"
          property="type_name"
        ></el-table-column>
        <el-table-column
          label="打印机类型"
          align="center"
          property="type_name"
        ></el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="del(scope.row.setting_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </Main>
      <Footer :total="totals">
        <el-pagination
          :page-size="10"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </div>

    <add-set v-if="shows == 'add'" @out="out" />
  </div>
</template>
<script>
import { DELPRINTS, SHOWPRINTS } from "../service";

export default {
  components: {
    addSet: () => import("./addSet")
  },
  data() {
    return {
      shows: "list",
      listData: [],
      totals: 0,
      pages: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async del(settingId) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELPRINTS({ setting_id: settingId }).then(res => {
            if (res.status === 200) {
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    async getList() {
      await SHOWPRINTS({ page: this.pages, pageshow: 10 }).then(res => {
        let { code, data } = res;
        if (code === 200) {
          this.totals = data.count;
          this.listData = data.data;
        } else {
          this.listData = [];
        }
      });
    },
    add() {
      this.shows = "add";
    },
    out() {
      this.getList();
      this.shows = "list";
    }
  }
};
</script>
<style scoped lang="scss"></style>
